import { CacheManager } from "../../com/gengine/manager/CacheManager";
import { ParamsConst } from "../../mortal/common/global/ParamsConst";
import { SenderType } from "./SenderType";
import { Log } from "../../com/gengine/debug/Log";
type int = number;
//class PHPSender

export class PHPSender {

  /* internal  */ static getShareObject(): void {
    if (PHPSender._myShareObject == null) {
      PHPSender._myShareObject = CacheManager.instance.getLocal(
        PHPSender.FRXZ_SO
      );
      PHPSender._myShareObject.addEventListener(
        flash.events.NetStatusEvent.NET_STATUS,
        PHPSender.onNetStatusEvent
      );
    }
    return;
  }

  /* internal  */ static onNetStatusEvent(
    arg1: Object /* flash.events.NetStatusEvent */
  ): void {
    return;
  }

  public static sendToURLByPHP(type: int, info: string = ""): void {
    // var str: string = null;
    // var typeInfo: string = null;

    // if (PHPSender.isSend(ParamsConst.instance.username, type, info) == false) {
    //   return;
    // }
    // try {
    //   if (
    //     !(null == ParamsConst.instance.dothingUrl) &&
    //     !(ParamsConst.instance.dothingUrl == "")
    //   ) {
    //     str = ParamsConst.instance.dothingUrl + "&do=" + type;
    //     str =
    //       str +
    //       ("&type=" +
    //         type +
    //         "&username=" +
    //         encodeURI(ParamsConst.instance.username) +
    //         "&phpID=" +
    //         ParamsConst.instance.phpID);
    //     if (info == "") {
    //       typeInfo = SenderType.getInfoByType(type);
    //       str =
    //         str +
    //         ("&info=" +
    //           encodeURI(
    //             type + "->" + ParamsConst.instance.username + "->" + typeInfo
    //           ));
    //     } else {
    //       str = str + ("&info=" + encodeURI(type + "->" + info));
    //     }
    //     PHPSender.addQueue(new Object(/* flash.net.URLRequest */ str));
    //   }
    // } catch (e) {
    //   Log.system(e.message);
    // }
    return;
  }

  /* internal  */ static addQueue(
    arg1: Object /* flash.net.URLRequest */
  ): void {
    var loc1 = flash.utils.getTimer();
    var loc2 = -PHPSender._nextTime;
    if (loc1 > PHPSender._nextTime) {
      flash.net.sendToURL(arg1);
      PHPSender._nextTime = loc1 + PHPSender.Max_Time;
    } else {
      flash.utils.setTimeout(
        flash.net.sendToURL,
        PHPSender._nextTime - loc1,
        arg1
      );
      PHPSender._nextTime = PHPSender._nextTime + PHPSender.Max_Time;
    }
    return;
  }

  public static isSend(username: string, type: int, info: string): Boolean {
    PHPSender.getShareObject();
    if (PHPSender._allUser == null) {
      PHPSender._allUser = PHPSender._myShareObject.data["userNameObject"];
      if (PHPSender._allUser == null) {
        PHPSender._allUser = {};
      }
    }
    if (PHPSender._userNameObject == null) {
      PHPSender._userNameObject =
        PHPSender._allUser[ParamsConst.instance.loginIP + "_" + username];
      if (PHPSender._userNameObject == null) {
        PHPSender._userNameObject = { time: ParamsConst.instance.time };
        PHPSender._allUser[ParamsConst.instance.loginIP + "_" + username] =
          PHPSender._userNameObject;
      }
    }
    if (PHPSender._userNameObject[type] == info) {
      return false;
    }
    PHPSender._userNameObject[type] = info;
    PHPSender._myShareObject.data.userNameObject = PHPSender._allUser;
    PHPSender._myShareObject.flush(10000);
    return true;
  }

  public static sendGift(arg1: int): void {
    if (
      !(ParamsConst.instance.loginGiftUrl == null) &&
      !(ParamsConst.instance.loginGiftUrl == "")
    ) {
      flash.net.sendToURL(
        new Object(
          /* flash.net.URLRequest */ ParamsConst.instance.loginGiftUrl +
            "&playerid=" +
            arg1
        )
      );
    }
    return;
  }

  public static clearUserName(arg1: string): void {
    var loc1 = null;
    var loc2 = null;
    var loc3 = null;
    PHPSender.getShareObject();
    if (PHPSender._allUser == null) {
      PHPSender._allUser = PHPSender._myShareObject.data["userNameObject"];
    }
    if (PHPSender._allUser) {
      loc1 = PHPSender._allUser[ParamsConst.instance.loginIP + "_" + arg1];
      if (loc1) {
        loc2 = loc1.time as Date;
        loc3 = ParamsConst.instance.time as Date;
        if (loc2.date != loc3.date) {
          delete PHPSender._allUser[ParamsConst.instance.loginIP + "_" + arg1];
          PHPSender._myShareObject.data.userNameObject = PHPSender._allUser;
          PHPSender._myShareObject.flush(10000);
        }
      }
    }
    return;
  }

  public static renrenAttention(): void {
    if (
      !(ParamsConst.instance.attentionUrl == null) &&
      !(ParamsConst.instance.attentionUrl == "")
    ) {
      flash.net.sendToURL(
        new Object(/* flash.net.URLRequest */ ParamsConst.instance.attentionUrl)
      );
    }
    return;
  }

  public static vipInfo360(arg1: int, arg2: int): void {
    var loc1 = null;
    if (
      !(ParamsConst.instance.vip360Url == null) &&
      !(ParamsConst.instance.vip360Url == "")
    ) {
      loc1 =
        ParamsConst.instance.vip360Url +
        "&time=" +
        arg1 +
        "&username=" +
        encodeURI(ParamsConst.instance.username) +
        "&vipType=" +
        arg2;
      flash.net.sendToURL(new Object(/* flash.net.URLRequest */ loc1));
    }
    return;
  }

  private static /* const */ FRXZ_SO: string = "frxz2_so";

  private static /* const */ Max_Time: int = 500;

  private static /* var */ _hasSend: Boolean = true;

  private static /* var */ _userNameObject: Object;

  private static /* var */ _allUser: Object;

  private static /* var */ _infoMap: Object;

  public static /* var */ isCreateRole: Boolean = false;

  private static /* var */ _myShareObject: Object /* flash.net.SharedObject */;

  private static /* var */ _nextTime: number = 0;
}

{
  PHPSender._hasSend = true;
  PHPSender._infoMap = {};
  PHPSender.isCreateRole = false;
  PHPSender._nextTime = 0;
}
